<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/news.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%!
	List searchTxzjNews(Input input) {
		Integer page = 1; // 页码
		Integer itemCount = input.getInt("item_count", 4); // 每一页条目
		String keywords = input.getDesc("keywords", ""); // 关键字
		Integer hospital = input.getInt("hospital", -1); // 医院编号
		Integer uuid = input.getInt("uid", -1);
		String sinceID = input.getDesc("since_id","0"); // 下拉刷新
		String maxID = input.getDesc("max_id","0"); // 上拉加载
		String newsId = "";
		String selectContent = "new map(n.id as id , n.newsId as news_id, n.url as url, n.title as title, n.summary as summary, n.time as time, n.img as img, n.jsonString as json, n.tag as tag, n.category as category, n.branchName as branch_name)";
		Where where = new Where();
		String order = "desc"; // 排序
		where.and("n.category!=", 0);
		if (!sinceID.equals("0")) {
			where.and("n.newsId > " + sinceID);
			order = "asc";
		} else if (!maxID.equals("0")) {
			where.and("n.newsId < " + maxID);
		}
		List newsList = QBuilder.db("basedb").select(selectContent).from("News n").where(where.and("n.hospital=", hospital).and("n.status = ", STATUS_ENABLE).and("n.title like '%" + keywords + "%' or n.summary like '%" + keywords + "%' or n.content like'%" + keywords + "%'")).order("n.newsId", order).page(itemCount, page).query().getResultObj();
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(NEWS_CATEGORY);
		oh.setOperate(LOVE);
		return love(query(newsList, oh), uuid, oh);
	}
%>
<%
	Integer uid = token.getUid();
	// 搜索科室与医生
	String keywords = input.getDesc("keywords", "-1").trim().replaceAll(" ", "").replace("%", "\\%").replace("_", "\\_").replace("\\", "\\\\\\\\");
	input.setDesc("keywords", keywords);
	if ("".equals(keywords)) {
		Tools.encryptPrint(out, new Message(30041, "请输入搜索条件"));
		return;
	}
	Integer type = input.getInt("type", 1);
	List<Map> depts = depts = QBuilder.db("basedb").select("distinct new map (h.id as id, h.name as dept_name,h.descr as hospital_name, h.hospital as dept_id, h.jsonString as json)").from("Hospital as h").where(new Where("h.status=", STATUS_ENABLE).and("concat(h.descr, h.name) like '%" + keywords + "%'").and("h.type=", 2)).query().getResultObj();
	List<Map> experts = QBuilder.db("basedb").select("distinct new map (u.id as id, u.name as name, u.avatar as avatar, u.title as title, h.name as dept_name,h.descr as hospital_name, h.hospital as dept_id, u.gender as gender, u.jsonString as json, u.type as type)").from("Hospital as h, User as u").where(new Where("h.status=", STATUS_ENABLE).and("h.hospital=u.deptId").and("u.type=", USER_EMPLOYEE).and("u.name like '%" + keywords + "%'").and("h.type=", 2).and("u.status=", STATUS_ENABLE)).query().getResultObj();
	List<Integer> myExperts = QBuilder.db("basedb").select("u.id").from("OperateHistory as o, Hospital as h, User as u").where(new Where("o.hospital=", hospital).and("o.status=", STATUS_ENABLE).and("o.uid=", uid).and("o.operate=", ATTENTION).and("o.category=", SECTION_DOCTOR).and("u.id=o.oid").and("h.status=", STATUS_ENABLE).and("h.hospital=u.deptId").and("u.type=", USER_EMPLOYEE)).query().getResultObj();
	List<Integer> myDepts = QBuilder.db("basedb").select("h.id").from("OperateHistory as o, Hospital as h").where(new Where("o.hospital=", hospital).and("o.status=", STATUS_ENABLE).and("o.uid=", uid).and("o.operate=", ATTENTION).and("o.category=", DEPT_CATEGORY).and("h.id=o.oid").and("h.status=", STATUS_ENABLE)).query().getResultObj();
	// 全局搜素	type=1
	for (Map map:depts) {
		Integer id = (Integer)map.get("id");
		map.put("attention", myDepts.contains(id)?1:0);
		Map jsonMap = JSONUtil.fromJson(map.remove("json") + "", new TypeReference<Map>(){});
		if (isEmpty(jsonMap))
			jsonMap = new HashMap();
		map.put("avatar", Tools.getDesc(jsonMap.get("avatar"), "http://cdn-img.easyicon.net/png/11907/1190716.gif"));
	}
	for (Map expert:experts) {
		Map jsonMap = JSONUtil.fromJson(expert.remove("json") + "", new TypeReference<Map>(){});
		if (isEmpty(jsonMap))
			jsonMap = new HashMap();
		expert.put("specialty", Tools.getDesc(jsonMap.get("specialty"), "暂无擅长"));
		expert.put("introduce", Tools.getDesc(jsonMap.get("introduce"), "暂无介绍"));
		Integer id = (Integer)expert.get("id");
		expert.put("attention", myExperts.contains(id)?1:0);
		User user = User.fromMap(expert, User.class);
		expert.put("avatar", getDefaultAvatar(user));
	}
	// 首页搜索
	if (type == 2) {
		Iterator<Map> it = depts.iterator();
		while (it.hasNext()) {
			Map m = it.next();
			Integer id = (Integer)m.get("id");
			boolean isEx =  myDepts.contains(id);
			if (!isEx)
				it.remove();
		}
		it = experts.iterator();
		while (it.hasNext()) {
			Map expert = it.next();
			Integer id = (Integer)expert.get("id");
			boolean isEx =  myExperts.contains(id);
			if (!isEx)
				it.remove();
		}
	}
	Map<String, Object> data = new HashMap<String, Object>();
	data.put("depts", depts);
	data.put("experts", experts);
	input.setDesc("uid", token.getUid() + "");
	data.put("news", searchTxzjNews(input));
	data.put("quests", search(input).get("data"));
	msg.put("data", data);
	Tools.encryptPrint(out, msg);
%>